package res

import (
	"encoding/json"
	"github.com/gorilla/websocket"
)

func SendConnFailWithMsg(msg string, conn *websocket.Conn) {
	data := Response{
		Code: FailValidCode,
		Data: empty,
		Msg:  msg,
	}
	byteData, _ := json.Marshal(data)
	conn.WriteMessage(websocket.TextMessage, byteData)
}

func SendConnOkWithData(data any, conn *websocket.Conn) {
	byteData, _ := json.Marshal(Response{
		Code: SuccessCode,
		Data: data,
		Msg:  "成功",
	})
	conn.WriteMessage(websocket.TextMessage, byteData)
}

func SendWsMsg(onlineMap map[uint]map[string]*websocket.Conn, userId uint, data any) {
	addrMap, ok := onlineMap[userId]

	if !ok {
		return

	}
	byteData, _ := json.Marshal(Response{
		Code: SuccessCode,
		Data: data,
		Msg:  "成功",
	})

	for _, conn := range addrMap {
		conn.WriteMessage(websocket.TextMessage, byteData)
	}

}
